*****************************************************************************
*********            Appendix Table 12 - First-Stage Estimates 
*********            	Heterogeneous Agent Model Split Sample IV
*****************************************************************************


local cutoff=0.01
clear
capture file close myfile
save ".\data\temp\temp1", replace emptyok
use ".\data\clean\laneuse_dataset_01dec14_I110N",clear
gen TT_dif_hr_110N=dist/MLspeed-dist/ELspeed
drop if TT_dif_hr_110N<0
collapse TT_dif_hr_110N, by(date hour min)
la var TT_dif_hr_110N "Travel Time Savings I-110N"
tempfile tt110N
save `tt110N'
use ".\data\clean\laneuse_dataset_2dec14_I110S",clear
gen TT_dif_hr_110S=dist/MLspeed-dist/ELspeed
drop if TT_dif_hr_110S<0
collapse TT_dif_hr_110S, by(date hour min)
la var TT_dif_hr_110S "Travel Time Savings I-110S"
tempfile tt110S
save `tt110S'
use ".\data\clean\I10E_laneuse_dataset_15nov14_wcensus.dta",clear
gen TT_dif_hr_10E=dist/MLspeed-dist/ELspeed
drop if TT_dif_hr_10E<0
collapse TT_dif_hr_10E, by(date hour min)
la var TT_dif_hr_10E "Travel Time Savings I-10E"
tempfile tt10E
save `tt10E'

use ".\data\clean\raw5mindata_I210.dta", clear
keep if type=="ML"
keep if dir=="W"
keep speed date hour min
collapse speed, by(date hour min)
g inv210speed=1/speed
la var inv210speed "Inverse Contemporaneous Speed I-210W"
keep date hour min inv210speed
tempfile sp210 
save `sp210'

use ".\data\clean\I10W_laneuse_dataset_15nov14_wcensus", clear

*merge m:1 date hour using "\\rschfs1x\userRS\A-E\arw227_RS\Documents\expresslanes\Spring2015\cleandata\HV_ML_reliab.dta", keep(1 3) nogen 
merge m:1 date hour using ".\data\clean\HV_ML_reliab_30days.dta", keep(1 3) nogen 
*merge m:1 date hour using ".\data\clean\HV_ML_reliab.dta", keep(1 3) nogen   // testing this 5/21 --didn't work
merge m:1 date hour min using `sp210', keep(1 3) nogen
merge m:1 date hour min using `tt110N', keep(1 3) nogen
merge m:1 date hour min using `tt110S', keep(1 3) nogen
merge m:1 date hour min using `tt10E', keep(1 3) nogen

gen reliabilityML=dist/p20_speedML-dist/p50_speedML
gen reliabilityHV=dist/p20_speedHV-dist/p50_speedHV
gen reliability_diff=reliabilityML-reliabilityHV

*WTP calculation
gen TT_dif_hr=dist/MLspeed-dist/ELspeed
gen WTP2=charged_toll/TT_dif_hr
*keep if WTP2>0&WTP2~=.
*keep if WTP2~=.
*keep if WTP2<0
drop if ELspeed==.
drop if TT_dif_hr==.
*drop if weekend==1
drop if holiday==1
la var TT_dif_hr "Time in Hours"
la var reliability_diff "Reliability"
drop if dow==0|dow==6
keep if acct_type=="PRIVATE"&occupancy~="HOV-3"
keep if hour>4 & hour<9
replace reliability_diff=0 if reliability_diff<`cutoff'
g exittime=hh(exit_time)+mm(exit_time)/60+ss(exit_time)/(60*60)
egen exittimemean=mean(exittime), by(acct_no)
g exittimediff=exittime-exittimemean
g TT_dif_hr2 =TT_dif_hr^2
egen ETT_dif_hr=mean(TT_dif_hr),by(dow hour min rt_id)
la var ETT_dif_hr "E[Time in Hours]"
egen id=group(date rt_id)
egen tid=group(date hour rt_id)

preserve
	collapse TT_dif_hr, by(tid date hour rt_id)
	egen t=group(date hour)
	tsset rt_id t
	g L1hr_TTdif=L.TT_dif_hr if date[_n-1]==date[_n]
	g F1hr_TTdif=F.TT_dif_hr if date[_n+1]==date[_n]
	tsset, clear
	drop t
	g dow=dow(date)
	egen t=group(rt_id hour dow)
	g week=week(date)
	tsset t week
	g L3wk_TTdif=L3.TT_dif_hr 
	g L2wk_TTdif=L2.TT_dif_hr 
	g L1wk_TTdif=L.TT_dif_hr 
	g F1wk_TTdif=F.TT_dif_hr 
	g F2wk_TTdif=F2.TT_dif_hr 
	g F3wk_TTdif=F3.TT_dif_hr 
	keep tid  L1hr_TTdif F1hr_TTdif L3wk_TTdif L2wk_TTdif L1wk_TTdif F1wk_TTdif F2wk_TTdif F3wk_TTdif
	tempfile temp
	save `temp'
restore
merge m:1 tid using `temp', keep(3) nogen
sort transponder
preserve
	keep acct_no
	duplicates drop
	sort acct_no
	outsheet acct_no using ".\data\temp\temp3.txt", replace comma non noq
restore


la var F1hr_TTdif "Travel Time (1 hour lead)"
la var F1wk_TTdif "Travel Time (1 week lead)"
la var F2wk_TTdif "Travel Time (2 week lead)"

keep if TT_dif_hr>0

gen count=1
egen freq=sum(count), by(acct_no)
drop if freq<5


egen temp1=mean(TT_dif_hr) if date>td(20oct2013), by(dow hour)
egen TT_dif_hr_meanpostoct=mean(temp1), by(dow hour)


egen TT_dif_hr_mean=mean(TT_dif_hr), by(dow hour)

 



qui ivreg2  charged_toll (TT_dif_hr= F1hr_TTdif F1wk_TTdif F2wk_TTdif) reliability_diff ///
	if TT_dif_hr>0, cluster(rt_id) first
local fstat=round(e(rkf),1)
qui reg TT_dif_hr F1hr_TTdif F1wk_TTdif F2wk_TTdif reliability_diff if TT_dif_hr>0, cluster(rt_id)
est sto b0
estadd scalar fstat=`fstat'


qui ivreg2  charged_toll (TT_dif_hr= TT_dif_hr_mean) reliability_diff ///
	if TT_dif_hr>0, cluster(rt_id) first
local fstat=round(e(rkf),1)
qui reg TT_dif_hr TT_dif_hr_mean reliability_diff if TT_dif_hr>0, cluster(rt_id)
est sto b1
estadd scalar fstat=`fstat'

qui ivreg2  charged_toll (TT_dif_hr= TT_dif_hr_meanpostoct) reliability_diff ///
	if TT_dif_hr>0, cluster(rt_id) first
local fstat=round(e(rkf),1)
qui reg TT_dif_hr TT_dif_hr_meanpostoct reliability_diff if TT_dif_hr>0, cluster(rt_id)
est sto b2
estadd scalar fstat=`fstat'

qui ivreg2  charged_toll (TT_dif_hr= inv210speed) reliability_diff ///
	if TT_dif_hr>0, cluster(rt_id) first
local fstat=round(e(rkf),1)
qui reg TT_dif_hr inv210speed reliability_diff if TT_dif_hr>0, cluster(rt_id)
est sto b3
estadd scalar fstat=`fstat'

qui ivreg2  charged_toll (TT_dif_hr= TT_dif_hr_10E) reliability_diff ///
	if TT_dif_hr>0, cluster(rt_id) first
local fstat=round(e(rkf),1)
qui reg TT_dif_hr TT_dif_hr_10E reliability_diff if TT_dif_hr>0, cluster(rt_id)
est sto b4
estadd scalar fstat=`fstat'

qui ivreg2  charged_toll (TT_dif_hr= TT_dif_hr_110N) reliability_diff ///
	if TT_dif_hr>0, cluster(rt_id) first
local fstat=round(e(rkf),1)
qui reg TT_dif_hr TT_dif_hr_110N reliability_diff if TT_dif_hr>0, cluster(rt_id)
est sto b5
estadd scalar fstat=`fstat'

qui ivreg2  charged_toll (TT_dif_hr= TT_dif_hr_110S) reliability_diff ///
	if TT_dif_hr>0, cluster(rt_id) first
local fstat=round(e(rkf),1)
qui reg TT_dif_hr TT_dif_hr_110S reliability_diff if TT_dif_hr>0, cluster(rt_id)
est sto b6
estadd scalar fstat=`fstat'

esttab  b0 b1 b2 b3 b4 b5 b6 using ".\results\appendix\tabs\ATCXXXXpb.csv", replace  ///
	cells(b(star fmt(%9.3f)) se(par)) star(* 0.10 ** 0.05 *** 0.01) stats(r2 N fstat,fmt(%9.3f %9.0g %9.3f) labels(R-squared N Fstat)) ///
 nonumbers nodepvars ///
	  label mtitles("Leads" "Mean I-10W Speeds" "Mean I-10W Speeds after Oct. 20" "I-10E Travel Time Savings" "I-110N Travel Time Savings" "I-110S Travel Time Savings")




